package cn.sciento.farm.devicemanager.gateway.model.downlink;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

import static cn.sciento.farm.devicemanager.common.constant.MqttConstant.INVERTER_CONTROL_BLOCK_ID;
import static cn.sciento.farm.devicemanager.common.constant.MqttConstant.INVERTER_CONTROL_TOPIC;

@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = false)
public class InverterControlData extends AbstractBlockData {

    private String eui;

    private Integer id;

    private Float givenPress;
    private Float hyperPress;
    private Float lowPress;
    private Integer vfdControl;
    private Integer vfdPeriod;
    private Integer vfdRefresh;


    @Override
    public String generateId() {
        return String.format(INVERTER_CONTROL_BLOCK_ID, eui);
    }

    @Override
    public String generateStatus() {
        return null;
    }

    @Override
    public MqttContent generate() {
        Map<String,Integer> map = new HashMap<>();
        map.put("id",id);
        if(givenPress != null){
            map.put("givenPress", (int) (givenPress*100));
        }
        if(hyperPress != null){
            map.put("hyperPress", (int) (hyperPress*100));
        }
        if(lowPress != null){
            map.put("lowPress", (int) (lowPress*100));
        }
        if(vfdControl != null){
            map.put("vfdControl",vfdControl);
        }
        if(vfdRefresh != null){
            map.put("vfdRefresh",vfdRefresh);
        }
        map.put("vfdPeriod",vfdPeriod);
        String payload = JSON.toJSONString(map);
        String topic = String.format(INVERTER_CONTROL_TOPIC, eui);
        return new MqttContent(topic,payload);
    }
}
